package org.jeecg.modules.xxgx.djgl.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.xxgx.djgl.dto.DjcfDto;
import org.jeecg.modules.xxgx.djgl.dto.IntelligentCodingMainDto;
import org.jeecg.modules.xxgx.djgl.dto.uploadFileDto;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdj;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdjAndMx;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdjmx;
import org.jeecg.modules.xxgx.djgl.service.IDjcl;
import org.jeecg.modules.xxgx.djgl.service.IUploadFile;
import org.jeecg.modules.xxgx.djgl.vo.IntelligentCodingVo;
import org.jeecg.modules.xxgx.djgl.vo.PreviewQRcodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;


/**
 * @Description: 待开单据
 * @Author: jeecg-boot
 * @Date: 2023-08-18
 * @Version: V1.0
 */
@Api(tags = "待开单据扩展")
@RestController
@RequestMapping("/xxgx/djgl/csXxDkdjkz")
@Slf4j
public class CsXxDkdjKzController {


    @Autowired
    private IUploadFile iUploadFile;

    @Autowired
    private IDjcl iDjcl;

    @Autowired
    private ObjectMapper objectMapper;


    @AutoLog(value = "待开单据-单据导入")
    @ApiOperation(value = "待开单据-单据导入", notes = "待开单据-单据导入")
    @PostMapping(value = "/uploadFile")
    public Result<String> uploadFile(HttpServletRequest request) {

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = null;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            file = entity.getValue();// 获取上传文件对象
        }
        String dtostr = request.getParameter("dto");
        uploadFileDto dto = null;
        try {
            dto = objectMapper.readValue(dtostr, uploadFileDto.class);
        } catch (Exception e){
            throw new JeecgBootException(e.getMessage());
        }
        if (file == null || dto == null) {
            return Result.error("上传文件有误！");
        }
        File FileValue = iUploadFile.uploadFile(file, dto);
        try {
            Files.delete(FileValue.toPath());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.OK("导入成功！");
    }


    @AutoLog(value = "待开单据-单据合并")
    @ApiOperation(value = "待开单据-单据合并", notes = "待开单据-单据合并")
    @PostMapping(value = "/orderCombineNoDetailCombineById")
    public Result<String> orderCombineNoDetailCombineById(String ids) {
        iDjcl.orderCombineNoDetailCombineById(ids);
        return Result.OK("合并成功！");
    }

    @AutoLog(value = "待开单据-明细合并")
    @ApiOperation(value = "待开单据-明细合并", notes = "待开单据-明细合并")
    @PostMapping(value = "/orderDetailCombineById")
    public Result<String> orderDetailCombineById(String ids, String combineRule, String negaRowNewOrderFlag) {
        iDjcl.orderDetailCombineById(ids, combineRule, negaRowNewOrderFlag);
        return Result.OK("合并成功！");
    }

    @AutoLog(value = "待开单据-撤销合并")
    @ApiOperation(value = "待开单据-撤销合并", notes = "待开单据-撤销合并")
    @PostMapping(value = "/orderCombineCancel")
    public Result<String> orderCombineCancel(String id) {
        return iDjcl.orderCombineCancel(id);
    }

    @AutoLog(value = "待开单据-撤销拆分")
    @ApiOperation(value = "待开单据-撤销拆分", notes = "待开单据-撤销拆分")
    @PostMapping(value = "/orderSplitCancel")
    public Result<String> orderSplitCancel(String id) {
        return iDjcl.orderSplitCancel(id);
    }

    @AutoLog(value = "待开单据-单据拆分")
    @ApiOperation(value = "待开单据-单据拆分", notes = "待开单据-单据拆分")
    @PostMapping(value = "/orderSplitById")
    public Result<String> orderSplitById(@RequestBody DjcfDto dto) {
        iDjcl.orderSplitById(dto);
        return Result.OK("拆分成功！");
    }

    @AutoLog(value = "根据ids查询明细列表")
    @ApiOperation(value = "待开单据-根据ids查询明细列表", notes = "待开单据-根据ids查询明细列表")
    @PostMapping(value = "/getDetailListBySqdIds")
    public Result<PageInfo<List<CsXxDkdjmx>>> getDetailListBySqdIds(String ids, String pageSize, String page) {
        PageInfo<List<CsXxDkdjmx>> detailListBySqdIds = iDjcl.getDetailListBySqdIds(ids, pageSize, page);
        return Result.OK(detailListBySqdIds);
    }

    @AutoLog(value = "待开单据-手工拆分保存")
    @ApiOperation(value = "待开单据-手工拆分保存", notes = "待开单据-手工拆分保存")
    @PostMapping(value = "/manualSplitOrderSave")
    public Result<CsXxDkdjAndMx> manualSplitOrderSave(@RequestBody CsXxDkdjAndMx csXxDkdjAndMx) {
        iDjcl.manualSplitOrderSave(csXxDkdjAndMx);
        return Result.OK(csXxDkdjAndMx);
    }

    @AutoLog(value = "待开单据-查看原单")
    @ApiOperation(value = "待开单据-获取原单列表", notes = "待开单据-获取原单列表")
    @PostMapping(value = "/getOriginalList")
    public Result<PageInfo<List<CsXxDkdj>>> getOriginalList(String id, String pageSize, String page) {
        PageInfo<List<CsXxDkdj>> originalList = iDjcl.getOriginalList(id, pageSize, page);
        return Result.OK(originalList);
    }

    @AutoLog(value = "待开单据-查看子单")
    @ApiOperation(value = "待开单据-获取子单列表", notes = "待开单据-获取子单列表")
    @PostMapping(value = "/getChildlList")
    public Result<PageInfo<List<CsXxDkdj>>> getChildlList(String id, String pageSize, String page) {
        PageInfo<List<CsXxDkdj>> childlList = iDjcl.getChildlList(id, pageSize, page);
        return Result.OK(childlList);
    }

    @AutoLog(value = "待开单据-票种变更")
    @ApiOperation(value = "待开单据-票种变更", notes = "待开单据-票种变更")
    @PostMapping(value = "/changeInvoiceType")
    public Result<PageInfo<List<CsXxDkdj>>> changeInvoiceType(@RequestParam(value = "ids") String ids, @RequestParam(value = "fplxdm") String fplxdm,
                                                              @RequestParam(value = "tspz") String tspz) {
        iDjcl.changeInvoiceType(ids, fplxdm, tspz);
        return Result.OK("变更成功！");
    }

    @AutoLog(value = "待开单据-生成开票二维码")
    @ApiOperation(value = "待开单据-生成开票二维码", notes = "待开单据-生成开票二维码")
    @PostMapping(value = "/previewQRcode")
    public Result<PreviewQRcodeVo> previewQRcode(@RequestParam(value = "id") String id) {
        PreviewQRcodeVo previewQRcodeVo = iDjcl.previewQRcode(id);
        return Result.OK(previewQRcodeVo);
    }

    @AutoLog(value = "待开单据-智能赋码")
    @ApiOperation(value = "待开单据-智能赋码", notes = "待开单据-智能赋码")
    @PostMapping(value = "/intelligentCoding")
    public Result<List<IntelligentCodingVo>> intelligentCoding(String id) {
        List<IntelligentCodingVo> intelligentCodingVos = iDjcl.intelligentCoding(id);
        return Result.OK(intelligentCodingVos);
    }

    @AutoLog(value = "待开单据-智能赋码保存")
    @ApiOperation(value = "待开单据-智能赋码保存", notes = "待开单据-智能赋码保存")
    @PostMapping(value = "/saveIntelligentCoding")
    public Result saveIntelligentCoding(@RequestBody IntelligentCodingMainDto dto) {
        iDjcl.saveIntelligentCoding(dto);
        return Result.OK("保存成功！");
    }

}
